﻿/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#ifndef MK_TYPE_H
#define MK_TYPE_H
#include <string>
#include <map>
#include <vector>
#include <list>
#include <queue>
#include <unordered_map>
#include <functional>

#ifdef _WIN32
#include <direct.h>
#include <WinSock2.h>
typedef int  socklen_t;
#ifdef MkSharedBuild
#define MkApi _declspec(dllexport)
#elif MkCoreImport
#define MkApi _declspec(dllimport)
#endif
#ifndef MkApi
#define MkApi
#endif
#else //_WIN32
#define MkApi
typedef int     SOCKET;
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#ifndef ERROR_SUCCESS
#define ERROR_SUCCESS  0
#define _strdup   strdup
#endif

#ifndef INVALID_SOCKET
#define INVALID_SOCKET  -1
#endif

#define closesocket close
#define ioctlsocket ioctl
#endif

//defines
#ifndef IN
#define IN
#endif

#ifndef INOUT
#define INOUT
#endif

#ifndef OUT
#define OUT
#endif

#ifndef TRUE
#define TRUE        1
#endif

#ifndef FALSE
#define FALSE       0
#endif

#ifndef BOOL
#define BOOL        int
#endif

#ifndef MAX_PATH
#define MAX_PATH 260
#endif

#define Len20M                      (1024*1024*20)
#define Len2M                       (1024*1024*2)
#define Len1M                       (1024*1024)
#define LenHalfM                    (512*1024)
#define Len20K                      20480
#define Len2K                       2048
#define Len1K                       1024
#define Len512                      512
#define Len256                      256
#define Len128                      128
#define Len64                       64
#define Len32                       32
#define Len16                       16
#define MtuSize                     1500
#define MaxInt32                    0x7FFFFFFF
#define MaxUint32                   0xFFFFFFFF
#define InvalidNum                  0xFFFFFFFF
#define InvalidSize                 0xFFFFFFFF
#define MkUnUse(p)                  (void)p

#define MkEndLine                   "\r\n"
#define MkUserAgent                 "mk system software"

#define MkDelete(p)         if(p) { delete p; p = nullptr; }
#define MkFree(p)           if(p) { free(p); p = nullptr; }
#define MkCloseFile(fp)     if(fp) { fclose(fp); fp = nullptr;}
#define MkCloseSocket(sock) if(INVALID_SOCKET != sock) { closesocket(sock); sock = INVALID_SOCKET;}
#define MkZero(p, size)     if(p) { memset(p, 0, size); }
#define MkMax(a, b)         a > b ? a : b;
#define MkMin(a, b)         a < b ? a : b;
#define MkSetDefaultStringValue(VariableName, strValue)  if (VariableName.empty()) { VariableName = strValue; }
#define MkSetStringValue(VariableName, strValue)  if (!strValue.empty() && VariableName != strValue) { VariableName = strValue; }
#define MkSetDefaultNumberValue(VariableName, iValue) if (0 == VariableName) { VariableName = iValue; }
#define MkSetNumberValue(VariableName, iValue) if (0 != iValue && iValue != VariableName) { VariableName = iValue; }



#if defined(_MSC_VER) // Microsoft Visual Studio
typedef __int64 Int64;
typedef unsigned __int64 Uint64;
#else                 // if defined(_MSC_VER) // Other platforms, use long long
typedef int64_t Int64;
typedef uint64_t Uint64;
#endif                // if defined(_MSC_VER)
typedef unsigned int        Uint32;
typedef unsigned short      Uint16;
typedef unsigned char       Uint8;

typedef int                 Int32;
typedef short               Int16;
typedef char                Int8;




typedef std::string         MkString;
#define MkVector            std::vector
#define MkList              std::list
#define MkMap               std::map
#define MkMultiMap          std::multimap
#define MkQueue             std::queue
#define MkUnorderedMap      std::unordered_map
#define MkFunction          std::function
#define MkPair              std::pair
typedef MkMap<MkString, MkString>  MkMapString;
typedef MkUnorderedMap<MkString, MkString> MkUnorderedMapString;
typedef MkMultiMap<MkString, MkString>   MkMultiMapString;
typedef MkVector<MkString>  MkVectorString;
typedef MkList<MkString>    MkListString;
typedef MkQueue<MkString>   MkQueueString;

typedef enum _mk_error_no
{
    NoneError = 0,
    CreateFolderFailed,
    InvalidUrl,
    InvalidTime,
    InvalidParams,
    SocketConnectFailed,
    QueueFull,
    QueueEmpty,
    QueueUinited,
    CreateThreadFailed,
    OpenFileFailed,
    OpenFloderFailed,
    SocketListenFailed,
    MallocFailed,
    TimeOut,
    CallbackSvrNotSet,
    InvalidId,
    IdIsUsed,
    SocketRecvFailed,
    SocketSendFailed,
    SocketSendCached,
    SocketAcceptFailed,
    SocketSetOptFailed,
    SocketIoctlFailed,
    SocketCreateFailed,
    SocketClosed,
    UnInited,
    SocketBindFailed,
    InvalidSession,
    UnSupported,
    RtmpError,
    NotHasEnoughBuffer,
    UnSupportProtocol,

    HttpRequestFailed = 150,
    ImageEmpty = 151,


    XmlErrorCodeBegin = 100000,
    XmlErrorNoAttribute,
    XmlErrorWrongAttributeType,
    XmlErrorFileNotFound,
    XmlErrorFileCouldNotBeOpened,
    XmlErrorFileReadError,
    XmlErrorParsingElement,
    XmlErrorParsingAttribute,
    XmlErrorParsingText,
    XmlErrorParsingCdata,
    XmlErrorParsingComment,
    XmlErrorParsingDeclaration,
    XmlErrorParsingUnknown,
    XmlErrorEmptyDocument,
    XmlErrorMismatchedElement,
    XmlErrorParsing,
    XmlErrorCanNotConverText,
    XmlErrorNoTextNode,
    XmlErrorElementDepthExceeded,
    XmlErrorCount,

    HttpResponseErrorBegin = 101000,
    RtspResponseErrorBegin = 102000,
    SipResponseErrorBegin = 103000,

    ErrorCodeUser = 200000,

    SystemBusy = 0xFFFFFFFC,
    ErrorCodeUsed = 0xFFFFFFFD,
    SystemError = 0xFFFFFFFE,
    UnknownError = 0xFFFFFFFF
} MkBaseErrorNo;

#endif
